package studentproject.main.controller;



import studentproject.main.domain.Student;
import studentproject.main.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public  class StudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService sts = new StudentService();
    public  void start(){
        loop:
        while (true) {
            System.out.println("==============欢迎来到学生管理系统==============");
            System.out.println("1.添加");
            System.out.println("2.删除");
            System.out.println("3.修改");
            System.out.println("4.查看");
            System.out.println("5.返回上一级");
            System.out.println("请输入您的选择");

            String choice = sc.next();
            switch (choice){
                case "1":
                    add();
                    break;
                case "2":
                    delete();
                    break;
                case "3":
                    update();
                    break;
                case "4":
                    query();
                    break;
                case "5":
                    break loop;
                default:
                    break;
            }
        }
    }

    private void update() {
        /*
        1.输入id，同时判断id是否存在
        T:重新输入
        N:放行
         */
        System.out.println("请输入id：");
        String id;
        int index;
        while (true){
            id = sc.next();
            index = sts.isExist(id);
            if(index == -1){
                System.out.println("id不存在，请重新输入。");
            }else {
                break;
            }
        }
        /*
        2.输入其他信息
         */
        Student student = get(id);

        boolean flag = sts.update(index,student);
        if(flag){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }

    private void delete() {
        /*
        1.输入id，同时判断id是否存在
        T:重新输入
        N:放行
         */
        System.out.println("请输入id：");
        String id;
        int index;
        while (true){
            id = sc.next();
            index = sts.isExist(id);
            if(index == -1){
                System.out.println("id不存在，请重新输入。");
            }else {
                break;
            }
        }

        boolean flag = sts.delete(index);
        if(flag){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }

    }

    private void query() {
        /*
        1.判断list是否不为空
        T：放行
        F：返回界面
         */
        boolean flag = sts.getList();
        if(!flag){
            System.out.println("当前没有信息，请添加后再尝试");
            return;
        }

        System.out.println("===============================");
        System.out.println("学号----姓名----年龄----生日----住址");
        outputInfo();
        System.out.println("===============================");
    }
    public void outputInfo(){
        ArrayList<Student> list = sts.find();
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(student != null){
                System.out.println(student.getId()+
                        "\t"+student.getName()+
                        "\t"+student.getAge()+
                        "\t"+student.getBirthday()+
                        "\t"+student.getAddress());
            }
        }
    }

    private void add() {
        /*
        1.输入id，同时判断id是否存在
        T:放行
        N:重新输入
         */
        System.out.println("请输入id：");
        String id;
        while (true){
            id = sc.next();
            int index = sts.isExist(id);
            if(index == -1){
                break;
            }else {
                System.out.println("id已存在，请重新输入。");
            }
        }
        /*
        2.输入其他信息
         */
        Student student = get(id);

        boolean flag = sts.add(student);
        if(flag){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

    }

    private Student get(String id){
        System.out.println("请输入您的姓名：");
        String name = sc.next();
        System.out.println("请输入您的年龄：");
        String age = sc.next();
        System.out.println("请输入您的生日：");
        String birthday = sc.next();
        System.out.println("请输入您的住址：");
        String address = sc.next();

        Student stu = new Student(id,name,age,birthday,address);
        return stu;
    }

}
